using System;
using System.Collections.Generic;
using CoreMobile.cmd;
using VABMobile.cmd;
using VABMobile.print;

namespace VABMobile.oo
{
    public class Turno : IParameter, PrintableDocument
    {
        private string id;
        private Bus bus;
        private Persona chofer1;
        private Persona chofer2;
        private Persona auxiliar;
        private Itinerario itinerario;

        public Turno(string id, Bus bus, Persona chofer1, Persona chofer2, Persona auxiliar, Itinerario itinerario)
        {
            this.Id = id;
            this.Itinerario = itinerario;
            this.Chofer2 = chofer2;
            this.Auxiliar = auxiliar;
            this.Chofer1 = chofer1;
            this.Bus = bus;
        }

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public Bus Bus
        {
            get { return bus; }
            set { bus = value; }
        }

        public Persona Chofer1
        {
            get { return chofer1; }
            set { chofer1 = value; }
        }

        public Persona Chofer2
        {
            get { return chofer2; }
            set { chofer2 = value; }
        }

        public Persona Auxiliar
        {
            get { return auxiliar; }
            set { auxiliar = value; }
        }

        public Itinerario Itinerario
        {
            get { return itinerario; }
            set { itinerario = value; }
        }
        /*
        public string asParameterLine(Command protocolCommand)
        {
            throw new NotImplementedException();
        }

        public IParameter fromParameterLine(string[] parameters)
        {
            throw new NotImplementedException();
        }*/

        #region Implementation of PrintableDocument

        public string initialBarCode()
        {
            return null;
        }

        public string[] bodyText()
        {
            string[] head = {
                                "Servicio:    " + this.id
                                , "Bus:            " + this.bus.Patente
                                , "1er Chofer:     " + (this.chofer1==null?"--":chofer1.ppRut())
                                , "2do Chofer:     " + (this.chofer2==null?"--":chofer2.ppRut())
                                , "Auxiliar:       " + this.auxiliar.ppRut()
                                //,"Itinerario:     "+itinerario
                                , "================================"
                            };
            string[] body = this.printVABs();
            string[] tail = { "================================" 
                            , "Hora Cierre:     " + DateTime.Now.ToString("HH:mm d/M/yyyy")};

            var full=new string[head.Length + body.Length + tail.Length];
            Array.Copy(head,full,head.Length);
            Array.Copy(body,0,full,head.Length,body.Length);
            Array.Copy(tail, 0, full, head.Length + body.Length, tail.Length);
            return full;
        }

        private string[] printVABs()
        {
            var vabs=new List<string>();
            vabs.Add("Pasajero          Monto");
            double total = 0;
            foreach (Asiento seat in this.bus.Asientos)
            {

                foreach (var boleto in seat.Boletos)
                {
                    if (boleto != null && boleto.Monto > 0)
                    {
                        vabs.Add(boleto.Numero + "  $" + (int)boleto.Monto);
                        total += boleto.Monto;
                    }
                }

            }
            vabs.Add("================================");
            vabs.Add("\x1b|bC\x1b|TOTAL         $"+(int)total);
            return vabs.ToArray();
        }

        public string finalBarCode()
        {
            return null;
        }

        #endregion
    }
}